package com.example.trackcircuitdiagnosis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.trackcircuitdiagnosis.dto.TrackDTO;
import com.example.trackcircuitdiagnosis.entity.TrackEntity;
import com.example.trackcircuitdiagnosis.service.TrackSerivce;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/track")
public class TrackController {


    @Autowired
    private TrackSerivce trackSerivce;

    // 新增或者更新
    @PostMapping
    @Operation(description = "保存轨道信息")
    public boolean save(@RequestBody TrackEntity track) {
        return trackSerivce.saveOrUpdate(track);
    }

    @DeleteMapping("/{id}")
    @Operation(
            description = "删除轨道信息",
            parameters = {
                    @Parameter(name = "id", description = "要删除的轨道信息id")
            }

    )
    public Boolean delete(@PathVariable Integer id) {
        return trackSerivce.removeById(id);
    }

    @PostMapping("/del/batch")
    @Operation(
            description = "批量删除轨道",
            parameters = {
                    @Parameter(name = "ids", description = "要批量删除的轨道信息")
            }

    )

    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return trackSerivce.removeByIds(ids);
    }

    @GetMapping
    @Operation(description = "查找全部轨道")
    public List<TrackEntity> findAll() {
        return trackSerivce.list();
    }

    @GetMapping("/{id}")
    @Operation(
            description = "查找轨道信息",
            parameters = {
                    @Parameter(name = "id", description = "要查找轨道id")
            }

    )
    public TrackEntity findOne(@PathVariable Integer id) {
        return trackSerivce.getById(id);
    }

    @GetMapping("/page")
    @Operation(
            description = "分页查询",
            parameters = {
                    @Parameter(name = "pageNum", description = "要查找轨道页数"),
                    @Parameter(name = "pageSize", description = "要查找轨道信息每一页的数量")
            }

    )
    public Page<TrackEntity> findPage(@RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize) {
        QueryWrapper<TrackEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("track_id");
        return trackSerivce.page(new Page<>(pageNum, pageSize), queryWrapper);
    }





    @GetMapping("/all")
    @Operation(description = "得到区段信息")
    public List<TrackDTO> getAllTracksWithAreasAndDevices() {
        List<TrackDTO> result = trackSerivce.getTracksWithAreasAndDevices();
        return result;
    }



}

